package com.gtw.modules.api.controller;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.mp.WeixinProxy;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.utils.R;
import com.gtw.modules.wx.AdvancedUtil;
import com.gtw.modules.wx2.wechat.qrcode.service.WechatQRCodeService;
import com.gtw.utils.Base64Image;
import com.gtw.utils.DownloadImage;
import com.gtw.utils.UidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;


/**
 * 获取微信access_toke
 * Created by wuye on 2018/5/21.
 */
@RestController
@RequestMapping("/api/aa")
public class ApiAuthController {

    @Autowired
    private WechatQRCodeService wechatQRCodeService;
    @Autowired
    private WeixinProxy weixinProxy;

    Logger logger = LoggerFactory.getLogger(ApiAuthController.class);

    /**
     * 个人二维码关注公众号
     *
     * @return
     */
    @AuthIgnoreApi
    @PostMapping("/findAccessUrl")
    public R findAccessToken(Integer pid) throws Exception {

        Map<String, Object> data = new HashMap<>();
        String accessToken = weixinProxy.getTokenManager().getAccessToken();
        logger.info("accessToken"+accessToken);
        /*//截取数据
        try {*/
            logger.info("pid"+pid);
            String ticket = wechatQRCodeService.createTempTicket(accessToken,2591000,pid);
            logger.info("ticket"+ticket);
            String url2 = wechatQRCodeService.showQrcode(accessToken, ticket);
            logger.info("url2"+url2);
            System.out.println(url2);
            data.put("code", 0);
            data.put("msg", "生成二维码成功");
            String name = UidUtil.getUUID();
            String path = "/usr/local/tm/img/";
//            String path = "/Users/wuye/Desktop/img";
            String bendiUrl = DownloadImage.download(url2,name+".jpg",path);
            logger.info("bendiUrl",bendiUrl);
            String urladdress2 = Base64Image.GetImageStr(bendiUrl);
            logger.info("urladdress2",urladdress2);
            return R.ok().put("data", urladdress2);
        /*} catch (Exception e) {
            data.put("code", -1);
            data.put("msg", "生成二维码失败");
            return R.ok().put("data", data);
        }*/
    }


    /**
     * 活动二维码关注公众号
     * @return
     */
    @AuthIgnoreApi
    @PostMapping("/attentionQr")
    public R attentionQr(String url) throws WeixinException {
        Map<String, Object> data = new HashMap<>();
        String accessToken = weixinProxy.getTokenManager().getAccessToken();

        try {
            String ticket = wechatQRCodeService.createForeverStrTicket(accessToken, url);
            String url2 = wechatQRCodeService.showQrcode(accessToken, ticket);
            data.put("code", 0);
            data.put("msg", "生成临时二维码成功");
            String name = UidUtil.getUUID();
            String bendiUrl = DownloadImage.download(url2,name+".jpg","/Users/wuye/wuyeDesktop/img");
            String urladdress2 = Base64Image.GetImageStr(bendiUrl);
            return R.ok().put("data", urladdress2);
        } catch (Exception e) {
            data.put("code", -1);
            data.put("msg", "生成临时二维码失败");
            return R.ok().put("data", data);

        }
    }
//        String accessToken = AdvancedUtil.getWeixinCgiToken("wxde9b9b1d6c4cc2df", "0ea12b593a6e81c756afe3813675c1cb");
//        // 网页授权接口访问凭证
//        String url2 = AdvancedUtil.getWeixinShorurl(url,accessToken);
//        JSONObject jsonObject = AdvancedUtil.getWeixinQrcode(url2,accessToken);
//        String ticket = jsonObject.getString("ticket");
//        String str = URLEncoder.encode(ticket,"utf-8");
//        String urladdress = AdvancedUtil.getWeixinImg(str);
//        String name = UidUtil.getUUID();
//        String bendiUrl = DownloadImage.download(urladdress,name+".jpg","/Users/wuye/Desktop/img");
//        String urladdress2 = Base64Image.GetImageStr(bendiUrl);
//        return R.ok().put("data",urladdress2);
}
